#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include<string>
#include<vector>

//变为函数指针
#define COUNT 10
typedef void*(*funtion)(void *);

class threads
{
public:
   //线程要线程的id ， 线程对应的函数 , 线程的传参buffer
   pthread_t _tid;
   funtion _fut;
   std::string _buffer;
};

void *handler(void* handler)
{
    
    int cnt = 0;

    // 10秒后回收
    while(true)
    {
       std::cout << "i am new thread" << " " << "my name is" << static_cast<const char*>(handler) << std::endl;
       sleep(1);
       cnt++;
       if(cnt == 10)
       break;
    }
    // 这里面不要传零时的参数
    pthread_exit((void *)"get signal of pthread_exit");

}

int main()
{
    std::vector<threads*> threadVector;
    //测试一下给进程发信号线程会怎么样
    for(int i = 1 ; i <= COUNT ; i++)
    {
        pthread_t tid;
        char buffer[64];
        snprintf(buffer,sizeof buffer,"thread ：%d", i);

        threads * newthrads = new threads();
        newthrads->_tid = 0;
        newthrads->_fut = handler;
        // 这里有没有问题存疑
        std::string newstring(buffer);
        newthrads->_buffer = newstring;
        
        int n = pthread_create(&newthrads->_tid ,nullptr, newthrads->_fut,(void *) ( newthrads->_buffer.c_str() ));

        // 这才是维护多个线程的管理方式
        threadVector.push_back(newthrads);
    }

    int i = 0;
    while( i < COUNT)
    {
         void * ret = nullptr;
         pthread_join(threadVector[i]->_tid,&ret);
         std::cout << (char*)ret << std::endl;
         i++;
    }

    while(true)
    {
       std::cout << "i am middle thread" << std::endl;
       sleep(1);
    }

    return 0;
}